package com.sun.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.sun.pojo.Employee;
import com.sun.service.EmployeeService;
import com.sun.utils.DataGridViewResult;
import com.sun.utils.SystemConstant;
import com.sun.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        Employee employee = employeeService.findEmployeeByLoginName(username,password);
        if(employee!=null){
            map.put(SystemConstant.SUCCESS,true);
            session.setAttribute(SystemConstant.LOGINUSER,employee);
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"用户名或密码错误");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/checkDeptHasEmployee")
    public String checkDeptHasEmployee(int id){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = employeeService.checkDeptHasEmployee(id);
        if(i>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该部门存在员工,无法删除!");
        }else{
            map.put(SystemConstant.EXIST,false);
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/checkRoleHasEmployee")
    public String checkRoleHasEmployee(int id){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = employeeService.checkRoleHasEmployee(id);
        if(i>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该角色存在员工,无法删除!");
        }else{
            map.put(SystemConstant.EXIST,false);
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/findEmployeeList")
    public DataGridViewResult findEmployeeList(EmployeeVo employeeVo){
        PageInfo pageInfo = employeeService.findEmployeeList(employeeVo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @PostMapping("/addEmployee")
    public String addEmployee(Employee employee,HttpSession session){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = employeeService.addEmployee(employee,session);
        if(i>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"["+employee.getLoginName()+"] 添加成功!");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"["+employee.getLoginName()+"] 添加失败!");
        }
        return JSON.toJSONString(map);
    }

    @PostMapping("/updateEmployee")
    public String updateEmployee(Employee employee,HttpSession session){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = employeeService.updateEmployee(employee,session);
        if(i>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"["+employee.getLoginName()+"] 修改成功!");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"["+employee.getLoginName()+"] 修改失败!");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/deleteEmployee")
    public String deleteEmployee(int id){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i=employeeService.deleteEmployee(id);
        if(i>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功!");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败!");
        }
        return JSON.toJSONString(map);
    }
    @PostMapping("/resetPassword")
    public String resetPassword(int id,HttpSession session){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i=employeeService.resetPasswordEmployee(id,session);
        if(i>0){
            map.put(SystemConstant.MESSAGE,"重置密码成功!");
        }else{
            map.put(SystemConstant.MESSAGE,"重置密码失败!");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/addEmployeeAndRole")
    public String addEmployeeAndRole(int id,String checkedRole){
        HashMap<String, Object> map = new HashMap<String, Object>();
        Boolean flag=employeeService.addEmployeeAndRole(id,checkedRole);
        if(flag){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"角色分配成功!");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"角色分配失败!");
        }
        return JSON.toJSONString(map);
    }
}
